package com.tedu.jsd2407_ivos_fzj.dict.service.impl;

import com.tedu.jsd2407_ivos_fzj.dict.mapper.DictMapper;
import com.tedu.jsd2407_ivos_fzj.dict.pojo.dto.DictParam;
import com.tedu.jsd2407_ivos_fzj.dict.pojo.dto.DictQuery;
import com.tedu.jsd2407_ivos_fzj.dict.pojo.entity.Dict;
import com.tedu.jsd2407_ivos_fzj.dict.pojo.vo.DictVO;
import com.tedu.jsd2407_ivos_fzj.dict.service.DictService;
import com.tedu.jsd2407_ivos_fzj.dictoption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictOptionMapper dictOptionMapper;

    @Override
    public List<DictVO> select(DictQuery dictQuery) {
        log.debug("查询业务，参数：{}",dictQuery);
        List<DictVO> dictVOList = dictMapper.select(dictQuery);
        return dictVOList;

    }

    @Override
    public void save(DictParam dictParam) {
        log.debug("保存业务，参数：{}",dictParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictParam,dict);
        if(dictParam.getId()==null){
            //新增
            dict.setCreateTime(new Date());
            dictMapper.insert(dict);
        }else{
            //更新
            dict.setUpdateTime(new Date());
            dictMapper.update(dict);
        }

    }

    @Override
    public void deleteById(Long id) {
        log.debug("根据id删除业务，参数：{}",id);

        dictMapper.deleteById(id);
        dictOptionMapper.deleteByDictId(id);
    }
}
